package yxy.game.pm2.command.union.tech;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.module.RedisLock;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.playtype.PlayTypeHandle;
import yxy.game.pm2.module.playtype.PlayTypeModule;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;
import yxy.game.pm2.module.union.player.UnionPlayer;
import yxy.game.pm2.module.union.player.tech.UnionPlayerTech;
import yxy.game.pm2.module.union.player.tech.UnionTechHandle;

import java.util.Collection;

/** 联盟科技 联盟科技重置 */
@MessageType(proto = Command.UnionTechReset, classtype = UnionMessage.UnionTechResetRequest.class)
public class CMD_UnionTechReset extends Command<UnionMessage.UnionTechResetRequest> {

	@Override
	public void execute(MessageSource messageSource, UnionMessage.UnionTechResetRequest request) {
		String playerCode = messageSource.getTag();
		int dataId = request.getDataId();

		UnionMessage.UnionTechResetResponse.Builder response = UnionMessage.UnionTechResetResponse.newBuilder();

		UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
		Result result = new UnionHandle().checkPlayerUnion(entry);
		if (!result.equals(Result.UnionIn)) {
			send(messageSource, response, result);
			return;
		}
		RedisLock uLock = UnionBean.lock(entry.getUnionBean().getCode());
		RedisLock pLock = UnionPlayer.lock(entry.getPlayerCode());
		try {
			if (uLock.tryLock() && pLock.tryLock()) {
				UnionBean union = entry.reloadUnionBean();
				UnionPlayer unionPlayer = entry.reloadUnionPlayer();
				UnionPlayerTech playerTech = unionPlayer.getTech();

				Currency cost = playerTech.resetcost();
				Collection<Currency> back = playerTech.resetback(dataId);
				if (back == null) {
					send(messageSource, response, Result.ParamError);
					return;
				}
				try {
					new CurrencyHandle().cost(playerCode, CCReason.联盟科技, cost);
				} catch (CurrencyNotEnoughException e) {
					send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
					return;
				}
				new CurrencyHandle().addCurrency(playerCode, CCReason.联盟科技, back);

				response.setTech(new UnionTechHandle().packTech(playerTech.reset(dataId)));
				response.addAllBack(new CurrencyHandle().packItems(back));

				unionPlayer.set();

				send(messageSource, response, Result.Success);

				PlayTypeHandle.saveUnion(playerCode, PlayTypeModule.get(playerCode));
				new BattleHandle().powerUpdate(playerCode);
				new UnionHandle().powerUpdate(union);
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			uLock.unlock();
			pLock.unlock();
		}
	}

	private void send(MessageSource messageSource, UnionMessage.UnionTechResetResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}